package control
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import com.greensock.TweenMax;
	
	//import events.AddMouseTouchEvent;
	//import events.MouseTouchEvent;
	import events.MyEvent;
	import flash.events.MouseEvent;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	public class ScrollPicBanner_ve extends EventDispatcher
	{
		//protected var mouseEventHolder:AddMouseTouchEvent;
		//protected var rootEventHolder:AddMouseTouchEvent;
		protected var startMouseY:Number;
		protected var startY:Number;
		protected var endMouseY:Number;
		protected var target:DisplayObject;
		public var distance:Number=0;
		protected var area:Rectangle;
		protected var finalY:Number;
		protected var _run:Boolean=true;
		public function ScrollPicBanner_ve(_target:DisplayObject,_area:Rectangle,ie:IEventDispatcher=null)
		{
			super(ie);
			target=_target;
			area=_area;
			
			target.addEventListener(MouseEvent.MOUSE_DOWN,handleBannerMouseDown,false,0,true);
			//mouseEventHolder=new AddMouseTouchEvent(target);
			//mouseEventHolder.addEventListener(mouseEventHolder.PRESS_DOWN,handleBannerMouseDown,false,0,true);
			//rootEventHolder=new AddMouseTouchEvent(target.root.stage);
		}
		
		public function get run():Boolean
		{
			return _run;
		}
		
		public function set run(value:Boolean):void
		{
			_run = value;
		}
		
		protected function handleBannerMouseDown(e:MouseEvent):void
		{
			if(!run)return;
			this.dispatchEvent(new MyEvent("moved", null, true));
			startMouseY = e.stageY;
			startY=target.y;
			target.root.stage.addEventListener(MouseEvent.MOUSE_MOVE,handleBannerMouseMove,false,0,true);
			target.root.stage.addEventListener(MouseEvent.MOUSE_UP,handleBannerMouseUp,false,0,true);
			//rootEventHolder.addEventListener(mouseEventHolder.PRESS_UP, handleBannerMouseUp,false,0,true);
			//rootEventHolder.addEventListener(mouseEventHolder.PRESS_MOVE, handleBannerMouseMove,false,0,true);
		}
		
		protected function handleBannerMouseMove(e:MouseEvent):void
		{
			if(!run)
			{
				handleBannerMouseUp();
				return;
			}
			this.dispatchEvent(new MyEvent("moved", null, true));
			endMouseY = e.stageY
			
			distance = startMouseY - endMouseY;
			moveFunction();
		}
		
		protected function moveFunction():void
		{
			
			if(!run)return;
			var _target:Number = target.y - distance;
			if(startY>_target)
			{
				if (_target < area.y + area.height - target.height) 
				{
					_target=area.y + area.height - target.height-100;
				}
			}
			else if(startY<_target)
			{
				if (_target > area.y )
				{
					_target = area.y+100;
				}
				
			}
			TweenMax.to(target, 0.5, { y:_target} );
			if (_target < area.y + area.height - target.height) _target = area.y + area.height - target.height;
			if (_target > area.y )_target = area.y;
			finalY=_target;
		}
		
		public function back():void
		{
			TweenMax.to(target,0.5,{y:finalY});
		}
		
		protected function handleBannerMouseUp(e:MouseEvent=null):void
		{
			
			endMouseY = e.stageY;
			distance = startMouseY - endMouseY;
			
			back();
			target.root.stage.removeEventListener(MouseEvent.MOUSE_MOVE,handleBannerMouseMove);
			target.root.stage.removeEventListener(MouseEvent.MOUSE_UP,handleBannerMouseUp);
			//rootEventHolder.removeEventListener(mouseEventHolder.PRESS_UP, handleBannerMouseUp);
			//rootEventHolder.removeEventListener(mouseEventHolder.PRESS_MOVE, handleBannerMouseMove);
			
		}
		
		public function gc():void
		{
			target.removeEventListener(MouseEvent.MOUSE_DOWN,handleBannerMouseDown);
			//rootEventHolder.removeAllEvent();
			//mouseEventHolder.removeAllEvent();
			//mouseEventHolder.removeEventListener(mouseEventHolder.PRESS_DOWN,handleBannerMouseDown);
		}
	}
}